package com.anhry.www.safety.manage.oa.struts.action;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.www.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.www.safety.manage.oa.bean.TbOaCallMsg;
import com.anhry.www.safety.manage.oa.bean.TbOaCallMsgView;
import com.anhry.www.safety.manage.oa.bean.TbOaNotice;
import com.anhry.www.safety.manage.oa.bean.TbOaNoticeFj;
import com.anhry.www.safety.manage.oa.bean.TbOaNoticeUser;
import com.anhry.www.safety.manage.oa.struts.base.OABaseAction;
import com.anhry.www.safety.util.Loggers;
import com.anhry.www.safety.util.Util;

/**
 * 
 * @author chen
 * @date 2012-8-20
 */
@Controller
@Scope("prototype")
@SuppressWarnings("unchecked")
public class CorpOaAction extends OABaseAction {

	/**
	 * 
	 */
	private static Logger logger = Logger.getLogger(CorpOaAction.class);

	private static final long serialVersionUID = 1L;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");

	private List<TbOaNoticeUser> inNoitceList;// 已收通知通告列表

	private String search;

	private String keyWord;

	private String beginTime;

	private String keyTemp;

	private Long id;

	private String endTime;

	private TbOaNoticeUser noticeUser;

	private List<TbOaNoticeUser> noticeUserList;

	private TbOaNotice notice;

	private List<TbOaNoticeFj> fjList; // 通知通告附件列表

	private List<TbOaCallMsgView> callMsgList;

	private String keyTime;

	private boolean deleOk;

	private String[] ids;

	private TbOaCallMsg callMsg;
	
	
	private String isRead;

	// 新增属性

	/**
	 * 收到的提醒
	 * 
	 * @return
	 */
	public String perCallInList() {
		String key = "";
		TbCorpInfo corp = (TbCorpInfo) this.contextPvd
				.getSessionAttr(Util.SESSION_CORPUSER);
		try {
//			if (null != this.getSearch() && ("1").equals(this.getSearch())) {
//				key = this.getKeyWord();
//			} else {
//				key = this.contextPvd.decode(this.getKeyWord()); // 解码
//			}
			key = this.getKeyWord();
			List callSize = this.callService.loadCallMsgList(key, this
					.getBeginTime(), this.getEndTime(), corp, isRead, 0, 0);
			if (null != callSize) {
				rowCount = Integer.parseInt(callSize.get(0).toString());
			}

			setPagingParameters();
			this.setCallMsgList(this.callService.loadCallMsgList(key, this
					.getBeginTime(), this.getEndTime(), corp, isRead, page
					.getStartRow(), page.getSize()));
//			if (null != key && !("").equals(key)) {
//				this.setKeyTemp(key);
//				this.setKeyWord(URLEncoder.encode(key, "utf-8"));// 编码
//			}
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("CorpOaAction类中perCallList()方法：" + e.getMessage());
		}
		this.setResult("call_per_inList");
		return SUCCESS;
	}

	/**
	 * 已收到通知
	 * 
	 * @return
	 */
	public String noticeInList() {
		try {
			String key = "";
			key = this.getKeyWord();
			
//			key = this.contextPvd.decode(this.getKeyWord()); // 解码
			
			TbCorpInfo corp = (TbCorpInfo) this.contextPvd
					.getSessionAttr(Util.SESSION_CORPUSER);
			List noticeSize = this.noticeService.loadInNoticeList(corp, this
					.getBeginTime(), this.getEndTime(), key,this.isRead, 0, 0);
			if (null != noticeSize) {
				rowCount = Integer.parseInt(noticeSize.get(0).toString());
			}
			setPagingParameters();
			this.setInNoitceList(this.noticeService.loadInNoticeList(corp, this
					.getBeginTime(), this.getEndTime(), key,this.isRead,
					page.getStartRow(), page.getSize()));
//			if (null != key && !("").equals(key)) {
//				this.setKeyTemp(key);
//				this.setKeyWord(URLEncoder.encode(key, "utf-8"));// 编码
//			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("CorpOaAction中noticeInList()方法：" + e);
		}
		setResult("notice_inList");
		return "success";
	}

	/**
	 * 收到提醒详细信息查看
	 * 
	 * @return
	 */
	public String perCallInShow() {
		TbOaCallMsg msg = null;
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this
					.getKeyWord())));
			if (null != this.getId() && !"".equals(this.getId())) {
				msg = (TbOaCallMsg) this.utilService.getObjectById(
						TbOaCallMsg.class, this.getId());
				if (null != msg) {
					if ("0".equals(msg.getIsRead())) {
						msg.setIsRead("1");
						msg.setReadTime(new Date());
						this.utilService.update(msg);
					}
					this.setCallMsg(msg);
					this.setResult("call_per_inshow");
					return SUCCESS;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("CorpOaAction类中perCallList()方法：" + e.getMessage());
		}

		return ERROR;
	}

	/**
	 * 已收通知查看
	 * 
	 * @return
	 */
	public String noticeInShow() {
		TbOaNotice notice = null;
		TbOaNoticeUser noticeU = null;
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this
					.getKeyWord())));
			if (null != this.getId() && !"".equals(this.getId())) {
				noticeU = (TbOaNoticeUser) this.utilService.getObjectById(
						TbOaNoticeUser.class, this.getId());
				if (null != noticeU) {
					notice = (TbOaNotice) this.utilService.getObjectById(
							TbOaNotice.class, noticeU.getTbOaNotice().getId());
					if (null != notice) {
						if ("0".equals(noticeU.getIsRead())) {
							noticeU.setIsRead("1");// 标记为已读
							noticeU.setReadTime(new Date());
							this.utilService.update(noticeU);
						}
						this.setNoticeUser(noticeU);
						this.setNotice(notice);
						this
								.setFjList((List<TbOaNoticeFj>) this.utilService
										.getHqlQuery("from TbOaNoticeFj fj left join fetch fj.tbOaNotice where fj.tbOaNotice.id='"
												+ notice.getId() + "'"));
						setResult("notice_inShow");
						return "success";
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("CorpOaAction中noticeInShow()方法：" + e);
		}

		return ERROR;
	}

	/**
	 * 已收到提醒删除
	 * 
	 * @return
	 */
	public String callMsgDele() {
		try {
			this.setKeyWord(this.contextPvd.encode(this.contextPvd.decode(this
					.getKeyWord())));
			if (null != this.getIds() && !"".equals(this.getIds())) {
				String[] arr = this.getIds();
				for (String str : arr) {
					TbOaCallMsg call = (TbOaCallMsg) this.utilService
							.getObjectById(TbOaCallMsg.class, Long
									.parseLong(str));
					if (null != call) {
						call.setIsCorper("1");
						this.utilService.update(call);
					}
				}
				this.setTip("删除成功!");
				this.setDeleOk(true);
				return this.perCallInList();
			}
		} catch (Exception e) {
			e.printStackTrace();
			logger.info("CorpOaAction类中callMsgDele()方法：" + e.getMessage());
		}
		return ERROR;
	}

	// 获取 未读 消息个数
	public void getNotRead() {
		TbCorpInfo corp = (TbCorpInfo) this.contextPvd
				.getSessionAttr(Util.SESSION_CORPUSER);
		try {
			int size = this.callService.getNotReadCallSize(corp);
			this.contextPvd.getResponse().getWriter().print(size);
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("CallAction中getNotRead()方法：+" + e);
		}
	}

	public void loadUserNotice() {
		this.getContextPvd().getResponse().setContentType(
				"text/xml; charset=UTF-8");
		this.getContextPvd().getResponse().setHeader("Cache-Control",
				"no-cache");
		// String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		try {
			StringBuffer sbf = new StringBuffer();
			TbCorpInfo corp = (TbCorpInfo) this.contextPvd
					.getSessionAttr(Util.SESSION_CORPUSER);
			List<TbOaNoticeUser> notice = (List<TbOaNoticeUser>) this.utilService
					.getHqlQuery("from TbOaNoticeUser notice left join fetch notice.tbUser where notice.tbCorpInfo.id='"
							+ corp.getId()
							+ "' and notice.isRead=0 and notice.isDele=0 and notice.userType=1");
			if (null != notice && notice.size() > 0) {
				for (int i = 0; i < notice.size(); i++) {
					sbf.append(notice.get(i).getId() + ","
							+ notice.get(i).getTbOaNotice().getNoticeTitle()
							+ ";");
				}
				sbf.setLength(sbf.length() - 1);
			}
			this.contextPvd.getResponse().getWriter().write(sbf.toString());
			this.contextPvd.getResponse().getWriter().flush();
			this.contextPvd.getResponse().getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.info("CorpOaAction LoadUserMsg()方法：" + e);
		}
	}

	public String getKeyWord() {
		return keyWord;
	}

	public void setKeyWord(String keyWord) {
		this.keyWord = keyWord;
	}

	public String getKeyTemp() {
		return keyTemp;
	}

	public void setKeyTemp(String keyTemp) {
		this.keyTemp = keyTemp;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public String getKeyTime() {
		return keyTime;
	}

	public void setKeyTime(String keyTime) {
		this.keyTime = keyTime;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public List<TbOaCallMsgView> getCallMsgList() {
		return callMsgList;
	}

	public void setCallMsgList(List<TbOaCallMsgView> callMsgList) {
		this.callMsgList = callMsgList;
	}

	public boolean isDeleOk() {
		return deleOk;
	}

	public void setDeleOk(boolean deleOk) {
		this.deleOk = deleOk;
	}

	public TbOaCallMsg getCallMsg() {
		return callMsg;
	}

	public void setCallMsg(TbOaCallMsg callMsg) {
		this.callMsg = callMsg;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public List<TbOaNoticeFj> getFjList() {
		return fjList;
	}

	public void setFjList(List<TbOaNoticeFj> fjList) {
		this.fjList = fjList;
	}

	public List<TbOaNoticeUser> getInNoitceList() {
		return inNoitceList;
	}

	public void setInNoitceList(List<TbOaNoticeUser> inNoitceList) {
		this.inNoitceList = inNoitceList;
	}

	public TbOaNotice getNotice() {
		return notice;
	}

	public void setNotice(TbOaNotice notice) {
		this.notice = notice;
	}

	public TbOaNoticeUser getNoticeUser() {
		return noticeUser;
	}

	public void setNoticeUser(TbOaNoticeUser noticeUser) {
		this.noticeUser = noticeUser;
	}

	public List<TbOaNoticeUser> getNoticeUserList() {
		return noticeUserList;
	}

	public void setNoticeUserList(List<TbOaNoticeUser> noticeUserList) {
		this.noticeUserList = noticeUserList;
	}

	public SimpleDateFormat getSdf() {
		return sdf;
	}

	public void setSdf(SimpleDateFormat sdf) {
		this.sdf = sdf;
	}

	public SimpleDateFormat getSdf2() {
		return sdf2;
	}

	public void setSdf2(SimpleDateFormat sdf2) {
		this.sdf2 = sdf2;
	}

	public String getIsRead() {
		return isRead;
	}

	public void setIsRead(String isRead) {
		this.isRead = isRead;
	}

	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	 

}
